package com.neu.canteen.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neu.canteen.dao.CustomerDao;
import com.neu.canteen.domain.Customer;
import com.neu.canteen.domain.FoodType;
import com.neu.canteen.utils.JdbcUtil;

public class CustomerDaoImpl implements CustomerDao{
 
	@Override
	public Customer login(String loginName,String password) {
 
		//DBUtils
		
		Connection conn = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		
		Customer customer=null;
		try {
			conn = JdbcUtil.getConn();
			
			String sql = "select *from customer where login_name = ? and password = ?";
			 
			preparedStatement = conn.prepareStatement(sql);
			
			preparedStatement.setString(1, loginName);
			preparedStatement.setString(2, password);
			
			resultSet = preparedStatement.executeQuery();

			 
			if(resultSet.next()){
				
				customer = new Customer();
				customer.setId(resultSet.getInt("id"));
				customer.setName(resultSet.getString("name"));
				customer.setImg(resultSet.getString("img"));
				customer.setPhone(resultSet.getString("phone"));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.close(resultSet,preparedStatement,conn);
		}
		return customer;
	}
	
	
	@Override
	public List<Customer> findAll() {
		
		List<Customer> customers = new ArrayList<Customer>();
		
		Connection conn = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			conn = JdbcUtil.getConn();
			
			preparedStatement =  conn.prepareStatement("select *from customer");
			
			resultSet = preparedStatement.executeQuery();

			Customer customer = null;
			while(resultSet.next()){
				
				customer = new Customer();
				
				customer.setId(resultSet.getInt("id"));
				customer.setName(resultSet.getString("name"));
				customer.setLoginName(resultSet.getString("login_name"));
				customer.setPassword(resultSet.getString("password"));
				customer.setPhone(resultSet.getString("phone"));
				customer.setImg(resultSet.getString("img"));
				customers.add(customer);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.close(resultSet,preparedStatement,conn);
		}
		return customers;
	}
	@Override
	public int add(Customer customer){
		Connection conn=null;
		PreparedStatement preparedStatement=null;
		try {
			conn=JdbcUtil.getConn();
			String sql="insert into customer(name,login_name,password,img,phone) value (?,?,?,?,?)";
			preparedStatement=conn.prepareStatement(sql);
			preparedStatement.setString(1,customer.getName());
			preparedStatement.setString(2,customer.getLoginName());
			preparedStatement.setString(3,customer.getPassword());
			preparedStatement.setString(4,customer.getImg());
			preparedStatement.setString(5,customer.getPhone());
			return preparedStatement.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			JdbcUtil.close(null, preparedStatement, conn);
		}
		return -1;
	}	
	@Override
	public int deleteCustomoerId(int id) {
		 
		Connection conn =null;
		PreparedStatement preparedStatement=null;
		try {
			conn = JdbcUtil.getConn();
			
			String sql = "delete from customer where id = ?";
			preparedStatement = conn.prepareStatement(sql);
			
			preparedStatement.setInt(1, id);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.close(null, preparedStatement, conn);
		}
		return -1;
	}
	@Override
	public int updataCustomoerId(Customer customer){
		Connection connection=null;
		PreparedStatement preparedStatement=null;
		try {
			connection=JdbcUtil.getConn();
			String sql=("UPDATE customer SET name=?,login_name=?,password=?,img=?,phone=? WHERE id=?" );
			preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setString(1, customer.getName());
			preparedStatement.setString(2, customer.getLoginName());
			preparedStatement.setString(3, customer.getPassword());
			preparedStatement.setString(4, customer.getImg());
			preparedStatement.setString(5, customer.getPhone());
			preparedStatement.setInt(6, customer.getId());
			return preparedStatement.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JdbcUtil.close(null, preparedStatement, connection);
		}
		return -1;	
	}
}